/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricshorizontalpainter.h"
#include "configmanager.h"

static LyricsPainterManager::Register<LyricsHorizontalPainter> painter("Horizontal");

void LyricsHorizontalPainter::drawLyrics(QPainter& painter, LyricsTime time) {
    if (m_drawState != DrawLyrics)
        return;

    const LyricsData& lyrics = *m_lyrics;
    // find current lyrics
    int pos = lyrics.find(time);
    // if nothing found, exit
    if (pos == lyrics.size())
        return;

    ConfigManager* cfg = ConfigManager::getInstance();

    // determine where should i print the current line of lyrics
    int timeLength = (pos + 1 == lyrics.size()) ? 0 :
        (lyrics[pos + 1].offset - lyrics[pos].offset);
    int printX = m_rect.left() + m_rect.width() * cfg->value("Style/HorizontalLyricsFactor", 0.5).toDouble() -
            ((timeLength == 0) ? 0 : getTextWidth(lyrics[pos].lyrics) * (time - lyrics[pos].offset) / timeLength);
    int printY = m_rect.top() + (m_rect.height() - m_fontMetrics->height()) / 2 + m_fontMetrics->ascent();

    // current lyrics changed
    if (m_midLyrics != pos) {
        m_midLyrics = pos;
        updateInterval(getTextWidth(lyrics[pos].lyrics), timeLength);
    };

    // find the first line of lyrics i need to print 
    while (printX > 0 && pos > 0) {
        --pos;
        printX -= getTextWidth(lyrics[pos].lyrics);
    };

    // ok, now i print the lyrics line by line
    while (printX < m_rect.right() && pos < lyrics.size()) {
        if (pos == m_midLyrics) {
            // print highlighted line
            drawText(painter, printX, printY,
                getFadingColor(time - lyrics[pos].offset, timeLength),
                lyrics[pos].lyrics);
        } else {
            drawText(painter, printX, printY,
                cfg->value("Style/FgColor", Qt::darkGreen).value<QColor>(),
                lyrics[pos].lyrics);
        };
        printX += getTextWidth(lyrics[pos].lyrics);
        ++pos;
    };
    //m_painter.drawLine(width() / 2, 0, width() / 2, height());
    //m_painter.drawText(0, m_painter.fontMetrics().height() + m_painter.fontMetrics().ascent(), QString::number(time / 60000) + ":" + QString::number(time % 60000 / 1000));
    //m_painter.drawText(0, m_painter.fontMetrics().ascent(), QString("FPS = ") + QString::number(1000.0 / newInterval));
};

int LyricsHorizontalPainter::getTextWidth(const QString& str) {
    ConfigManager* cfg = ConfigManager::getInstance();
    int spacing = cfg->value("Style/HorizontalLyricsSpacing", 10).toInt();
    return m_fontMetrics->width(str) + spacing;
};

